#include <stdlib.h>
#include "CIpodLib.h"
#include "CIpodLog.h"
#include "CIpodChip.h"
#include "CIpodVehicle.h"
#include "CIpodSync.h"
#include "CIpodEvent.h"
#include "CIpodConfig.h"
#include "CIpodList.h"
#include "CIpodDevice.h"

CIpodLib *CIpodLib::p_Instance = NULL;
pthread_mutex_t CIpodLib::p_InstanceLibMutex;

CIpodLib::CIpodLib() {
    CIpodLog::i("CIpodLib::CIpodLib, version = %s, time = %s\n", IPOD_LIB_VERSION, IPOD_LIB_TIME);
}

CIpodLib::~CIpodLib() {
    if(p_Instance != NULL)
    {
        delete p_Instance;
        p_Instance = NULL;
    }
    pthread_mutex_destroy(&p_InstanceLibMutex);
    CIpodLog::i("CIpodLib::~CIpodLib(), ok \n");
}

CIpodLib *CIpodLib::getInstance() {
    if(NULL == p_Instance)
    {
        pthread_mutex_lock(&p_InstanceLibMutex);

        if(NULL == p_Instance)
        {
            p_Instance = new CIpodLib();
        }

        pthread_mutex_unlock(&p_InstanceLibMutex);
    }
    return p_Instance;
}

int CIpodLib::ipodLibInit(int mode ,int connMode) {
    CIpodLog::i("CIpodLib::ipodLibInit, mode = %d connMode = %d \n", mode,connMode);
    CIpodLog::init();
    CIpodVehicle::initCachePath();
    CIpodSync::init();
    pthread_mutex_init(&p_InstanceLibMutex,NULL);

    int ret = -1;
    ret = CIpodConfig::defaultConfig(mode, connMode);

    CIpodLog::i("CIpodLib::ipodLibInit, ret = %d, mode = %d \n", ret, mode);
    return ret;
}

int CIpodLib::ipodLibDeinit() {
    CIpodConfig::deinit();
    // CIpodChip::deinit();
    CIpodSync::deinit();
    pthread_mutex_init(&p_InstanceLibMutex,NULL);
    CIpodLog::i("CIpodLib::ipodLibDeinit, ok \n");
    return 0;
}

void CIpodLib::registerObserver(IPODObserver *observer) {

    if(observer != NULL)CIpodEvent::registerObserver(observer);
}

void CIpodLib::unregisterObserver() {
    CIpodEvent::unregisterObserver();
}

void CIpodLib::notifyBtAddress(const uint8 *address) {
    CIpodVehicle::setBtAddress(address);
}

void CIpodLib::notifyBtStatus(bool connected) {
    CIpodVehicle::setBtStatus(connected);
    CIpodSync::lock();
    std::list<CIpodDevice *> deviceList = CIpodList::getDeviceList();
    for (std::list<CIpodDevice *>::iterator iter = deviceList.begin(); iter != deviceList.end(); ++iter) {
        (*iter)->notifyBtStatus(connected);
    }
    CIpodSync::unlock();
}

void *startIPodConnFunc(void *arg)
{
    CIpodDevice* surp_ = (CIpodDevice*)arg;

    if(surp_->connectIap2() == 0)
    {
        CIpodLog::i("iAP2 Link Initialization has been successful.\n");
    }

    return NULL ;
}

int CIpodLib::connect(ipod_t *handle, const char *path)
{
    CIpodLog::i("CIpodLib::connect, path = %s\n", path);

    int ret = -1;
    CIpodSync::lock();

    if (CIpodList::isDeviceExist(path))
    {
        CIpodLog::e("iAP2 Device has already exist.\n");
        ret = 0;
    }
    else
    {
        CIpodDevice *device = new CIpodDevice(path);
        ret = device->create();
        if (ret == 0) {
            CIpodList::push(device);
            *handle = device->getHandle();
            CIpodLog::i( "An new device has been pushed into list successfully. handle: %d.\n", *handle );

            if(device->getConnMode() == ConnMode_BLUETOOTH || device->getConnMode() == ConnMode_WiFi )
            {
                pthread_t ID;
                pthread_create( &ID, NULL, startIPodConnFunc, device );
                pthread_detach(ID);
            }
            else
            {
                ret = device->connectIap2();
            }
        } else {
            CIpodLog::e("CIpodLib::connect, device->destroy! \n");
            device->destroy();
            CIpodConfig::deinit();
            delete device;
            CIpodLog::i("CIpodLib::connect, ipod create failed!\n");
        }
    }
    CIpodSync::unlock();
    return ret;
}

int CIpodLib::disconnect(ipod_t *handle) {
    CIpodLog::i("iAP2 device starts to disconnect handle: %d.\n", *handle );

    int ret = -1;
    CIpodSync::lock();
    if (CIpodList::isDeviceExist(*handle))
    {
        CIpodLog::i("iAP2 device exist and start pop handle: %d.\n", *handle );
        CIpodList::pop(*handle);
        *handle = -1;
        ret = 0;
        CIpodLog::i("iAP2 device disconnect successfully.\n");
    }
    else
    {
        CIpodLog::i("iAP2 device dose not exist handle: %d.\n", *handle );
    }
    CIpodSync::unlock();
    return ret;
}

int CIpodLib::notifyUSBAudioStatus(ipod_t handle, bool enabled) {
    CIpodLog::i("CIpodLib::notifyUSBAudioStatus, handle = %d, enabled = %d\n", handle, enabled);

    int ret = -1;
    CIpodSync::lock();
    CIpodDevice *device = CIpodList::getDevice(handle);
    if (device != NULL) {
        ret = device->notifyUSBAudioStatus(enabled);
        CIpodLog::i("CIpodLib::notifyUSBAudioStatus, ret = %d!\n", ret);
    } else {
        CIpodLog::i("CIpodLib::notifyUSBAudioStatus, ipod not exist!\n");
    }
    CIpodSync::unlock();
    return ret;
}

int CIpodLib::sendMediaCommand(ipod_t handle, int command) {
    CIpodLog::i("CIpodLib::sendMediaCommand, handle = %d, command = %d\n", handle, command);

    int ret = -1;
    CIpodSync::lock();
    CIpodDevice *device = CIpodList::getDevice(handle);
    if (device != NULL) {
        ret = device->sendMediaCommand(command);
        CIpodLog::i("CIpodLib::sendMediaCommand, ret = %d!\n", ret);
    } else {
        CIpodLog::i("CIpodLib::sendMediaCommand, ipod not exist!\n");
    }
    CIpodSync::unlock();
    return ret;
}

int CIpodLib::sendMediaCommandDown(ipod_t handle, int command) {
    CIpodLog::i("CIpodLib::sendMediaCommandDown, handle = %d, command = %d\n", handle, command);

    int ret = -1;
    CIpodSync::lock();
    CIpodDevice *device = CIpodList::getDevice(handle);
    if (device != NULL) {
        ret = device->sendMediaCommandDown(command);
        CIpodLog::i("CIpodLib::sendMediaCommandDown, ret = %d!\n", ret);
    } else {
        CIpodLog::i("CIpodLib::sendMediaCommandDown, ipod not exist!\n");
    }
    CIpodSync::unlock();
    return ret;
}

int CIpodLib::sendMediaCommandUp(ipod_t handle, int command) {
    CIpodLog::i("CIpodLib::sendMediaCommandUp, handle = %d, command = %d\n", handle, command);

    int ret = -1;
    CIpodSync::lock();
    CIpodDevice *device = CIpodList::getDevice(handle);
    if (device != NULL) {
        ret = device->sendMediaCommandUp(command);
        CIpodLog::i("CIpodLib::sendMediaCommandUp, ret = %d!\n", ret);
    } else {
        CIpodLog::i("CIpodLib::sendMediaCommandUp, ipod not exist!\n");
    }
    CIpodSync::unlock();
    return ret;
}

int CIpodLib::setPlayingTrackPosition(ipod_t handle, uint32 position) {
    CIpodLog::i("CIpodLib::setPlayingTrackPosition, handle = %d, position = %d\n", handle, position);

    int ret = -1;
    CIpodSync::lock();
    CIpodDevice *device = CIpodList::getDevice(handle);
    if (device != NULL) {
        ret = device->setPlayingTrackPosition(position);
        CIpodLog::i("CIpodLib::setPlayingTrackPosition, ret = %d!\n", ret);
    } else {
        CIpodLog::i("CIpodLib::setPlayingTrackPosition, ipod not exist!\n");
    }
    CIpodSync::unlock();
    return ret;
}

int CIpodLib::getShuffleMode(ipod_t handle, int *mode) {
    CIpodLog::i("CIpodLib::getShuffleMode, handle = %d\n", handle);

    int ret = -1;
    CIpodSync::lock();
    CIpodDevice *device = CIpodList::getDevice(handle);
    if (device != NULL) {
        ret = device->getShuffleMode(mode);
        CIpodLog::i("CIpodLib::getShuffleMode, ret = %d!\n", ret);
    } else {
        CIpodLog::i("CIpodLib::getShuffleMode, ipod not exist!\n");
    }
    CIpodSync::unlock();
    return ret;
}

int CIpodLib::getRepeatMode(ipod_t handle, int *mode) {
    CIpodLog::i("CIpodLib::getRepeatMode, handle = %d\n", handle);

    int ret = -1;
    CIpodSync::lock();
    CIpodDevice *device = CIpodList::getDevice(handle);
    if (device != NULL) {
        ret = device->getRepeatMode(mode);
        CIpodLog::i("CIpodLib::getRepeatMode, ret = %d!\n", ret);
    } else {
        CIpodLog::i("CIpodLib::getRepeatMode, ipod not exist!\n");
    }
    CIpodSync::unlock();
    return ret;
}

int CIpodLib::getPlayingTrackDuration(ipod_t handle, int * duration)
{
    int ret = -1;
    CIpodSync::lock();
    CIpodDevice *device = CIpodList::getDevice(handle);
    if (device != NULL) {
        ret = device->getPlayingTrackDuration(duration);
        CIpodLog::d("CIpodLib::getPlayingTrackDuration, ret = %d!\n", ret);
    } else {
        CIpodLog::e("CIpodLib::getPlayingTrackDuration, ipod not exist!\n");
    }
    CIpodSync::unlock();
    return ret;
}

int CIpodLib::getPlayingTrackTitle(ipod_t handle, std::string &title) {
    CIpodLog::d("CIpodLib::getPlayingTrackTitle, handle = %d\n", handle);

    int ret = -1;
    CIpodSync::lock();
    CIpodDevice *device = CIpodList::getDevice(handle);
    if (device != NULL) {
        ret = device->getPlayingTrackTitle(title);
        CIpodLog::d("CIpodLib::getPlayingTrackTitle, ret = %d!\n", ret);
    } else {
        CIpodLog::e("CIpodLib::getPlayingTrackTitle, ipod not exist!\n");
    }
    CIpodSync::unlock();
    return ret;
}

int CIpodLib::getPlayingTrackAlbum(ipod_t handle, std::string &album) {
    CIpodLog::d("CIpodLib::getPlayingTrackAlbum, handle = %d\n", handle);

    int ret = -1;
    CIpodSync::lock();
    CIpodDevice *device = CIpodList::getDevice(handle);
    if (device != NULL) {
        ret = device->getPlayingTrackAlbum(album);
        CIpodLog::d("CIpodLib::getPlayingTrackAlbum, ret = %d!\n", ret);
    } else {
        CIpodLog::e("CIpodLib::getPlayingTrackAlbum, ipod not exist!\n");
    }
    CIpodSync::unlock();
    return ret;
}

int CIpodLib::getPlayingTrackArtist(ipod_t handle, std::string &artist) {
    CIpodLog::d("CIpodLib::setPlayingTrackPosition, handle = %d\n", handle);

    int ret = -1;
    CIpodSync::lock();
    CIpodDevice *device = CIpodList::getDevice(handle);
    if (device != NULL) {
        ret = device->getPlayingTrackArtist(artist);
        CIpodLog::d("CIpodLib::getPlayingTrackArtist, ret = %d!\n", ret);
    } else {
        CIpodLog::e("CIpodLib::getPlayingTrackArtist, ipod not exist!\n");
    }
    CIpodSync::unlock();
    return ret;
}

int CIpodLib::getPlayingTrackAlbumArtwork(ipod_t handle) {
    CIpodLog::d("CIpodLib::getPlayingTrackAlbumArtwork, handle = %d\n", handle);

    int ret = -1;
    CIpodSync::lock();
    CIpodDevice *device = CIpodList::getDevice(handle);
    if (device != NULL) {
        ret = device->getPlayingTrackAlbumArtwork();
        CIpodLog::d("CIpodLib::getPlayingTrackAlbumArtwork, ret = %d!\n", ret);
    } else {
        CIpodLog::e("CIpodLib::getPlayingTrackAlbumArtwork, ipod not exist!\n");
    }
    CIpodSync::unlock();
    return ret;
}

int CIpodLib::getPlayStatus(ipod_t handle, IPODPlayStatus *status) {
    CIpodLog::i("CIpodLib::getPlayStatus, handle = %d\n", handle);

    int ret = -1;
    CIpodSync::lock();
    CIpodDevice *device = CIpodList::getDevice(handle);
    if (device != NULL) {
        ret = device->getPlayStatus(status);
        CIpodLog::i("CIpodLib::getPlayStatus, ret = %d!\n", ret);
    } else {
        CIpodLog::i("CIpodLib::getPlayStatus, ipod not exist!\n");
    }
    CIpodSync::unlock();
    return ret;
}

int CIpodLib::getPlayingTrackInfo(ipod_t handle, int32 *index, uint32 *count) {
    CIpodLog::i("CIpodLib::getPlayingTrackInfo, handle = %d\n", handle);

    int ret = -1;
    CIpodSync::lock();
    CIpodDevice *device = CIpodList::getDevice(handle);
    if (device != NULL) {
        ret = device->getPlayingTrackInfo(index, count);
        CIpodLog::i("CIpodLib::getPlayingTrackInfo, ret = %d!\n", ret);
    } else {
        CIpodLog::i("CIpodLib::getPlayingTrackInfo, ipod not exist!\n");
    }
    CIpodSync::unlock();
    return ret;
}

int CIpodLib::getPlayingChapterInfo(ipod_t handle, int32 *index, uint32 *count) {
    CIpodLog::i("CIpodLib::getPlayingChapterInfo, handle = %d\n", handle);

    int ret = -1;
    CIpodSync::lock();
    CIpodDevice *device = CIpodList::getDevice(handle);
    if (device != NULL) {
        ret = device->getPlayingChapterInfo(index, count);
        CIpodLog::i("CIpodLib::getPlayingChapterInfo, ret = %d!\n", ret);
    } else {
        CIpodLog::i("CIpodLib::getPlayingChapterInfo, ipod not exist!\n");
    }
    CIpodSync::unlock();
    return ret;
}

int CIpodLib::isListLevelSupported(ipod_t handle, bool *supported) {
    CIpodLog::i("CIpodLib::isListLevelSupported, handle = %d\n", handle);

    int ret = -1;
    CIpodSync::lock();
    CIpodDevice *device = CIpodList::getDevice(handle);
    if (device != NULL) {
        ret = device->isListLevelSupported(supported);
        CIpodLog::i("CIpodLib::isListLevelSupported, ret = %d!\n", ret);
    } else {
        CIpodLog::i("CIpodLib::isListLevelSupported, ipod not exist!\n");
    }
    CIpodSync::unlock();
    return ret;
}

int CIpodLib::resetMusicList(ipod_t handle, uint32 *num) {
    CIpodLog::i("CIpodLib::resetMusicList, handle = %d\n", handle);

    int ret = -1;
    CIpodSync::lock();
    CIpodDevice *device = CIpodList::getDevice(handle);
    if (device != NULL) {
        ret = device->resetMusicList(num);
        CIpodLog::i("CIpodLib::resetMusicList, ret = %d!\n", ret);
    } else {
        CIpodLog::i("CIpodLib::resetMusicList, ipod not exist!\n");
    }
    CIpodSync::unlock();
    return ret;
}

int CIpodLib::selectMusicList(ipod_t handle, int32 index, int32 level, uint32 *num) {
    CIpodLog::i("CIpodLib::selectMusicList, handle = %d, index = %d, level = %d\n", handle, index, level);

    int ret = -1;
    CIpodSync::lock();
    CIpodDevice *device = CIpodList::getDevice(handle);
    if (device != NULL) {
        ret = device->selectMusicList(index, level, num);
        CIpodLog::i("CIpodLib::selectMusicList, ret = %d!\n", ret);
    } else {
        CIpodLog::i("CIpodLib::selectMusicList, ipod not exist!\n");
    }
    CIpodSync::unlock();
    return ret;
}

int CIpodLib::getMusicList(ipod_t handle, uint32 startIndex, uint32 readCount, IPODMusicList *musicList) {
    CIpodLog::i("CIpodLib::getMusicList, handle = %d, startIndex = %lu, readCount = %lu\n", handle, startIndex, readCount);

    int ret = -1;
    CIpodSync::lock();
    CIpodDevice *device = CIpodList::getDevice(handle);
    if (device != NULL) {
        ret = device->getMusicList(startIndex, readCount, musicList);
        CIpodLog::i("CIpodLib::getMusicList, ret = %d!\n", ret);
    } else {
        CIpodLog::i("CIpodLib::getMusicList, ipod not exist!\n");
    }
    CIpodSync::unlock();
    return ret;
}

int CIpodLib::getMusicList_c(ipod_t handle, uint32 startIndex, uint32 readCount, C_IPODMusicList *musicList) {
    CIpodLog::i("CIpodLib::getMusicList_c, handle = %d, startIndex = %lu, readCount = %lu\n", handle, startIndex, readCount);

    int ret = -1;
    CIpodSync::lock();
    CIpodDevice *device = CIpodList::getDevice(handle);
    if (device != NULL) {
        ret = device->getMusicList_c(startIndex, readCount, musicList);
        CIpodLog::i("CIpodLib::getMusicList, ret = %d!\n", ret);
    } else {
        CIpodLog::i("CIpodLib::getMusicList, ipod not exist!\n");
    }
    CIpodSync::unlock();
    return ret;
}

int CIpodLib::selectMusicPlayingList(ipod_t handle, int32 index) {
    CIpodLog::i("CIpodLib::selectMusicPlayingList, handle = %d, index = %d\n", handle, index);

    int ret = -1;
    CIpodSync::lock();
    CIpodDevice *device = CIpodList::getDevice(handle);
    if (device != NULL) {
        ret = device->selectMusicPlayingList(index);
        CIpodLog::i("CIpodLib::selectMusicPlayingList, ret = %d!\n", ret);
    } else {
        CIpodLog::e("CIpodLib::selectMusicPlayingList, ipod not exist!\n");
    }
    CIpodSync::unlock();
    return ret;
}

int CIpodLib::getMusicPlayingList(ipod_t handle, uint32 startIndex, uint32 readCount, C_IPODMusicList *musicList) {
    CIpodLog::d("CIpodLib::getMusicPlayingList, handle = %d, startIndex = %lu, readCount = %lu\n", handle, startIndex, readCount);

    int ret = -1;
    CIpodSync::lock();
    CIpodDevice *device = CIpodList::getDevice(handle);
    if (device != NULL) {
        ret = device->getMusicPlayingList(startIndex, readCount, musicList);
        CIpodLog::d("CIpodLib::getMusicPlayingList, ret = %d!\n", ret);
    } else {
        CIpodLog::e("CIpodLib::getMusicPlayingList, ipod not exist!\n");
    }
    CIpodSync::unlock();
    return ret;
}

int CIpodLib::launchApp(ipod_t handle, const char *appBundleID) {
    CIpodLog::i("CIpodLib::launchApp, handle = %d, appBundleID = %s\n", handle, appBundleID);
    if (appBundleID == NULL) {
        CIpodLog::e("CIpodLib::launchApp appBundleID is NULL!\n");
        return -1;
    }

    int ret = -1;
    CIpodSync::lock();
    CIpodDevice *device = CIpodList::getDevice(handle);
    if (device != NULL) {
        ret = device->launchApp(appBundleID);
        CIpodLog::i("CIpodLib::launchApp, ret = %d!\n", ret);
    } else {
        CIpodLog::i("CIpodLib::launchApp, ipod not exist!\n");
    }
    CIpodSync::unlock();
    return ret;
}

int CIpodLib::sendEAData(ipod_t handle, const uint8 *data, uint16 len) {
    int ret = -1;
    CIpodSync::lock();
    CIpodDevice *device = CIpodList::getDevice(handle);
    if (device != NULL) {
        ret = device->sendEAData(data, len);
    } else {
        CIpodLog::i("CIpodLib::sendEAData, ipod not exist!\n");
    }
    CIpodSync::unlock();
    return ret;
}

int CIpodLib::getEANativeState(ipod_t handle) {
    CIpodLog::i("CIpodLib::getEANativeState, handle = %d\n", handle);

    int ret = -1;
    CIpodSync::lock();
    CIpodDevice *device = CIpodList::getDevice(handle);
    if (device != NULL) {
        ret = device->getEANativeState();
        CIpodLog::i("CIpodLib::getEANativeState, ret = %d!\n", ret);
    } else {
        CIpodLog::i("CIpodLib::getEANativeState, ipod not exist!\n");
    }
    CIpodSync::unlock();
    return ret;
}

int CIpodLib::setLocationInformation(ipod_t handle, const char *sentence) {
    if (sentence == NULL) {
        CIpodLog::e("CIpodLib::setLocationInformation sentence is NULL!\n");
        return -1;
    }

    int ret = -1;
    CIpodSync::lock();
    CIpodDevice *device = CIpodList::getDevice(handle);
    if (device != NULL) {
        ret = device->setLocationInformation(sentence);
        CIpodLog::d("CIpodLib::setLocationInformation, ret = %d!\n", ret);
    } else {
        CIpodLog::i("CIpodLib::setLocationInformation, ipod not exist!\n");
    }
    CIpodSync::unlock();
    return ret;
}

int CIpodLib::wifiInformationSharing(WifiConfigurationInfo wifi_info) {
    CIpodLog::i("CIpodLib::wifiInformationSharing\n");
    CIpodVehicle::setWifiInformarion(wifi_info);
    return 0;
}

void CIpodLib::ipod_bt_spp_state_change(BtSppState state)
{
    CIpodLog::i("Rfcomm connect state changed :%d -> %d\n",CIpodVehicle::getbtSppConnStatus(),state);
    int last_connect_state = CIpodVehicle::getbtSppConnStatus();

    CIpodVehicle::setbtSppConnStatus(state);

    //上一次蓝牙处于连接态，但现在断开
    if (state != ipod_SppState_Connected && last_connect_state == ipod_SppState_Connected)
    {
        CIpodEvent::sendEvent(IPOD_EVT_CONNECT_STATUS_CHANGED, 0xFF, 0);
    }
}

void CIpodLib::ipodRegistBtSppSendData(BtSendData n_bt_spp_send_data)
{
    CIpodDevice::ipodRegistBtSppSendData(n_bt_spp_send_data);
}

void CIpodLib::setWiFiDataCallback( WiFiDataCallback callback )
{
    CIpodDevice::setWiFiDataCallback( callback );
}

int CIpodLib::ipod_BT_spp_data_pro(ipod_t handle,int cDataLen, const char * pData)
{
    int ret = -1;
//    CIpodSync::lock();
    CIpodDevice *device = CIpodList::getDevice(handle);
    if (device != NULL) {
        ret = device->ipod_BT_spp_data_pro(cDataLen, pData);
    } else {
        CIpodLog::i("CIpodLib::sendBtData, ipod not exist!\n");
    }
//    CIpodSync::unlock();
    return ret;
}

int CIpodLib::sendWiFiData( ipod_t handle,int cDataLen, const char * pData )
{
    int ret = -1;

    CIpodDevice *device = CIpodList::getDevice(handle);
    if (device != NULL) {
        ret = device->sendWiFiData(cDataLen, pData);
    } else {
        CIpodLog::i("CIpodLib::sendWiFiData, ipod not exist!\n");
    }

    return ret;
}

int CIpodLib::iap2CarPlayStartSession(ipod_t handle, int type){
    CIpodLog::d("CIpodLib::iap2CarPlayStartSession handle: %d, type: %d.\n", handle, type);

    int ret = -1;
    CIpodSync::lock();
    CIpodDevice *device = CIpodList::getDevice(handle);
    if (device != NULL) {
        ret = device->iap2CarPlayStartSession(type);
        CIpodLog::d("CIpodLib::iap2CarPlayStartSession, ret = %d!\n", ret);
    } else {
        CIpodLog::i("CIpodLib::iap2CarPlayStartSession, ipod not exist!\n");
    }
    CIpodSync::unlock();
    return ret;
}

int CIpodLib::iap2OOBBTPairingCompletionInformation(ipod_t handle,int resultCode ){
    CIpodLog::d("CIpodLib::iap2OOBBTPairingCompletionInformation, handle = %d, resultCode = %d\n", handle, resultCode);

    int ret = -1;
    CIpodSync::lock();
    CIpodDevice *device = CIpodList::getDevice(handle);
    if (device != NULL) {
        ret = device->iap2OOBBTPairingCompletionInformation(resultCode);
        CIpodLog::d("CIpodLib::iap2OOBBTPairingCompletionInformation, ret = %d!\n", ret);
    } else {
        CIpodLog::i("CIpodLib::iap2OOBBTPairingCompletionInformation, ipod not exist!\n");
    }
    CIpodSync::unlock();
    return ret;
}

int CIpodLib::iap2BluetoothPairingAccessoryInformation(ipod_t handle, const char * inPairingDataP192,const char * inPairingDataP256)
{
    CIpodLog::i("CIpodLib::iap2BluetoothPairingAccessoryInformation, handle = %d, inPairingDataP192 = %s inPairingDataP256 = %s\n", handle, inPairingDataP192,inPairingDataP256);

    int ret = -1;
    CIpodSync::lock();
    CIpodDevice *device = CIpodList::getDevice(handle);
    if (device != NULL) {
        ret = device->iap2BluetoothPairingAccessoryInformation(inPairingDataP192,inPairingDataP256);
        CIpodLog::i("CIpodLib::iapOOBBTPairingCompletionInformation, ret = %d!\n", ret);
    } else {
        CIpodLog::i("CIpodLib::iap2BluetoothPairingAccessoryInformation, ipod not exist!\n");
    }
    CIpodSync::unlock();
    return ret;
}

int CIpodLib::iap2BluetoothPairingStatus(ipod_t handle, int success, const char * cancelReason){
    CIpodLog::i("CIpodLib::iap2BluetoothPairingStatus, handle = %d, success = %d cancelReason:%s \n", handle, success,cancelReason);

    int ret = -1;
    CIpodSync::lock();
    CIpodDevice *device = CIpodList::getDevice(handle);
    if (device != NULL) {
        ret = device->iap2BluetoothPairingStatus(success,cancelReason);
        CIpodLog::i("CIpodLib::iap2BluetoothPairingStatus, ret = %d!\n", ret);
    } else {
        CIpodLog::i("CIpodLib::iap2BluetoothPairingStatus, ipod not exist!\n");
    }
    CIpodSync::unlock();
    return ret;
}
